package com.cn.kabalifemerchants.ui.order.fragment;

import com.cn.kabalifemerchants.api.Api;
import com.cn.kabalifemerchants.base.BasePresenter;
import com.cn.kabalifemerchants.callback.CacheCallBack;
import com.cn.kabalifemerchants.config.Constants;
import com.cn.kabalifemerchants.ui.login.bean.MsgBean;
import com.cn.kabalifemerchants.ui.order.bean.OrderListListBean;
import com.cn.kabalifemerchants.utils.LoggerUtils;
import com.lzy.okgo.OkGo;
import com.lzy.okgo.model.Response;

/**
 * Created by 晓林 on 2020/10/20.
 * function:
 */
public class OrderListPresenter extends BasePresenter<OrderListView> {

    public void onOrderGoodsListData(String token, int type, int page, int limit) {
        OkGo.<OrderListListBean>get(Api.API_ORDER_LIST_GOODS)
                .headers("token", token)
                .params("limit", limit)
                .params("page", page)
                .params("type", type)
                .execute(new CacheCallBack<OrderListListBean>() {
                    @Override
                    public void onSuccess(Response<OrderListListBean> response) {
                        super.onSuccess(response);
                        LoggerUtils.e("list：" + response.body());
                        if (isViewAttached())
                            if (response.body().getCode() == Constants.HTTP_SUCCESS)
                                getView().onOrderListSuccess(response.body());
                            else if (response.body().getCode() == Constants.HTTP_AGAIN_LOGIN)
                                getView().onReLoggedIn(response.body().getMsg());
                            else
                                getView().onError(response.body().getMsg());
                    }
                });
    }

    /**
     * 接单
     *
     * @param token
     * @param service_id
     * @param status
     */
    public void onOrderUpData(String token, int service_id, int status) {
        OkGo.<MsgBean>post(Api.API_ORDER_UP)
                .headers("token", token)
                .params("service_id", service_id)
                .params("status", status)
                .execute(new CacheCallBack<MsgBean>() {
                    @Override
                    public void onSuccess(Response<MsgBean> response) {
                        super.onSuccess(response);
                        LoggerUtils.e("list：" + response.body());
                        if (isViewAttached())
                            if (response.body().getCode() == Constants.HTTP_SUCCESS)
                                getView().onOrderUpSuccess(response.body());
                            else if (response.body().getCode() == Constants.HTTP_AGAIN_LOGIN)
                                getView().onReLoggedIn(response.body().getMsg());
                            else
                                getView().onError(response.body().getMsg());
                    }
                });
    }
    /**
     * 接单
     *
     * @param token
     * @param service_id
     */
    public void onOrderUpTuiData(String token, int service_id, int type) {
        OkGo.<MsgBean>post(Api.API_ORDER_UP_T)
                .headers("token", token)
                .params("service_id", service_id)
                .params("type", type)
                .execute(new CacheCallBack<MsgBean>() {
                    @Override
                    public void onSuccess(Response<MsgBean> response) {
                        super.onSuccess(response);
                        LoggerUtils.e("list：" + response.body());
                        if (isViewAttached())
                            if (response.body().getCode() == Constants.HTTP_SUCCESS)
                                getView().onOrderUpTuiSuccess(response.body());
                            else if (response.body().getCode() == Constants.HTTP_AGAIN_LOGIN)
                                getView().onReLoggedIn(response.body().getMsg());
                            else
                                getView().onError(response.body().getMsg());
                    }
                });
    }
    /**
     * 尾款
     *
     * @param token
     * @param service_id
     * @param balance
     */
    public void onOrderUpData(String token, int service_id, String balance) {
        OkGo.<MsgBean>post(Api.API_ORDER_UP)
                .headers("token", token)
                .params("service_id", service_id)
                .params("balance", balance)
                .execute(new CacheCallBack<MsgBean>() {
                    @Override
                    public void onSuccess(Response<MsgBean> response) {
                        super.onSuccess(response);
                        LoggerUtils.e("list：" + response.body());
                        if (isViewAttached())
                            if (response.body().getCode() == Constants.HTTP_SUCCESS)
                                getView().onOrderUpSuccess(response.body());
                            else if (response.body().getCode() == Constants.HTTP_AGAIN_LOGIN)
                                getView().onReLoggedIn(response.body().getMsg());
                            else
                                getView().onError(response.body().getMsg());
                    }
                });
    }

    /**
     * 取消 并退款。
     *
     * @param token
     * @param service_id
     */
    public void onOrderUpData(String token, int service_id) {
        OkGo.<MsgBean>post(Api.API_ORDER_UP_DEPOSIT)
                .headers("token", token)
                .params("service_id", service_id)
                .execute(new CacheCallBack<MsgBean>() {
                    @Override
                    public void onSuccess(Response<MsgBean> response) {
                        super.onSuccess(response);
                        LoggerUtils.e("list：" + response.body());
                        if (isViewAttached())
                            if (response.body().getCode() == Constants.HTTP_SUCCESS)
                                getView().onOrderUpSuccess(response.body());
                            else if (response.body().getCode() == Constants.HTTP_AGAIN_LOGIN)
                                getView().onReLoggedIn(response.body().getMsg());
                            else
                                getView().onError(response.body().getMsg());
                    }
                });
    }

    public void onOrderDetailsData(String token, int id) {
        OkGo.<String>post(Api.API_SREVUICE_ORDER_DETAILS)
                .headers("token", token)
                .params("id", id)
                .execute(new CacheCallBack<String>() {
                    @Override
                    public void onSuccess(Response<String> response) {
                        super.onSuccess(response);
                        LoggerUtils.e("服务订单详情：" + response.body());
//                        if (isViewAttached())
//                            if (response.body().getCode() == Constants.HTTP_SUCCESS)
//                                getView().onOrderDetailsSuccess(response.body());
//                            else if (response.body().getCode() == Constants.HTTP_AGAIN_LOGIN)
//                                getView().onReLoggedIn(response.body().getMsg());
//                            else
//                                getView().onError(response.body().getMsg());
                    }
                });
    }
}
